<?php
/**
 * 交易每日统计脚本
 *
 */
class Controller_Admin_Syndata extends Controller_Template
{
	const _reject_goods         = 0;//6375;
	protected $_statis;

	public function __construct($request, $response)
	{
		$this->_statis	= new Model_Admin_Syndata();
		$this->template = 'smarty:admin/home/default';
		parent::__construct($request, $response);
	}

	public function action_cyc()
	{
		$start_time	= strtotime('2011-12-01');

		$end_time	= strtotime(date('Y-m-d', time()));

		//$statis_im = new Statisim();

		$count	= ($end_time - $start_time)/86400;	
		
		for($i=0; $i< $count; $i++)
		{
			$start_time = date('Y-m-d', $start_time);
		
			//$statis_im->read_csv($start_time);

			//$this->action_index($start_time);

			//$this->action_member($start_time);

			//$this->action_merchants_goods($start_time);

			$this->action_top($start_time);
			sleep(2);
			$start_time	= strtotime("+1 day", strtotime($start_time));
		}
	}
	
	public function action_cyc1()
	{
		$start_time	= strtotime('2011-09-11');

		$end_time	= strtotime('2011-10-01');

		$count	= ($end_time - $start_time)/86400;	
		
		for($i=0; $i< $count; $i++)
		{
			$start_time = date('Y-m-d', $start_time);
		
			$this->action_top($start_time);
			sleep(2);
			$start_time	= strtotime("+1 day", strtotime($start_time));
		}
	}

	public function action_cyc2()
	{
		$start_time	= strtotime('2011-10-01');

		$end_time	= strtotime('2011-11-01');

		$count	= ($end_time - $start_time)/86400;	
		
		for($i=0; $i< $count; $i++)
		{
			$start_time = date('Y-m-d', $start_time);
		
			$this->action_top($start_time);
			sleep(2);
			$start_time	= strtotime("+1 day", strtotime($start_time));
		}
	}

	public function action_cyc3()
	{
		$start_time	= strtotime('2011-11-01');

		$end_time	= strtotime('2011-12-01');

		$count	= ($end_time - $start_time)/86400;	
		
		for($i=0; $i< $count; $i++)
		{
			$start_time = date('Y-m-d', $start_time);
		
			$this->action_top($start_time);
			sleep(2);
			$start_time	= strtotime("+1 day", strtotime($start_time));
		}
	}

	public function action_index($date = null)
	{
		$use_date	= trim($date);
		if($use_date)
		{
			$start_time	= strtotime($use_date);

			$end_time	= strtotime('+1 day', $start_time);
		}else{
			$start_time	= strtotime(date('Y-m-d', strtotime('-1 day')));

			$end_time	= strtotime(date('Y-m-d', time()));
		}
		/*
		//商品销量统计
		$datas	= $this->goods_transacts($start_time, $end_time);
		*/
		//店铺销量统计
		$shops	= $this->_statis->shop_transacts($start_time, $end_time);
		print_r($shops);exit;
		/*	
		$datas	= array_merge($datas,$shops);
		
		Model_Admin_Syndata::handle_transact_topdata($datas);
		//下单数统计	
		$query	= $this->get_unpaidcondition($start_time, $end_time);

		$data	= $this->_statis->syn_data($query, $type = 33);
		
		unset($query);
		*/
		
		//付款订单统计
		$query	= $this->get_paidcondition($start_time, $end_time);
		
		$data1	= $this->_statis->syn_data($query, $type = 34);
		
		/*
		unset($query);
		//已发货  交易成功  交易关闭统计
		$type = '65,66,73';
		
		$query	= $this->get_othercondition($type, $start_time, $end_time);
		
		$data2	= $this->_statis->syn_data($query, $type);
		
		$type	= '129';

		$query	= $this->get_refundcondition($type, $start_time, $end_time);
		
		$data3	= $this->_statis->syn_refunddata($query, $type, $start_time, $end_time);

		unset($query);
		
		$type	= '130,131';

		$query	= $this->get_refundcondition($type, $start_time, $end_time);

		$data4	= $this->_statis->syn_refunddata($query, $type, $start_time, $end_time);
*/
		if(empty($data))
			$data	= array();
		else	
			$data	= array_merge($data,array());

		if(!empty($data1))
			$data	= array_merge($data,$data1);

		if(!empty($data2))
			$data	= array_merge($data,$data2);

		if(!empty($data3))
			$data   = array_merge($data,$data3);
	
		if(!empty($data4))
			$data	= array_merge($data,$data4);

		$this->_statis->update_data($data, $start_time);
	}

	public function action_top($date = null)
	{
		$use_date	= trim($date);
		
		if($use_date)
		{
			$start_time	= strtotime($use_date);

			$end_time	= strtotime('+1 day', $start_time);
		
		}else{

			$start_time	= strtotime(date('Y-m-d', strtotime('-1 day')));

			$end_time	= strtotime(date('Y-m-d', time()));
		}

		$datas	= $this->goods_transacts($start_time, $end_time);

		$shops	= $this->_statis->shop_transacts($start_time, $end_time);
		
		$datas	= array_merge($datas,$shops);

		if(!empty($datas))
			Model_Admin_Syndata::handle_transact_topdata($datas);
	}

	public function action_member($date = null)
	{
		$use_date	= trim($date);
		
		if($use_date)
		{
			$start_time	= strtotime($use_date);

			$end_time	= strtotime('+1 day', $start_time);
		
		}else{

			$start_time	= strtotime(date('Y-m-d', strtotime('-1 day')));

			$end_time	= strtotime(date('Y-m-d', time()));
		}

		$rs_data		= $this->_statis->get_member_data($start_time, $end_time);

		if(!empty($rs_data))
			$this->_statis->update_memberdata($rs_data);
	}
	
	public function action_merchants_goods($date = null)
	{
		$use_date	= trim($date);
		
		if($use_date)
		{
			$start_time	= strtotime($use_date);

			$end_time	= strtotime('+1 day', $start_time);
		
		}else{

			$start_time	= strtotime(date('Y-m-d', strtotime('-1 day')));

			$end_time	= strtotime(date('Y-m-d', time()));
		}

		$rs_data		= $this->_statis->get_merchants_goods_data($start_time, $end_time);

		if(!empty($rs_data))
			$this->_statis->update_merchants_gdata($rs_data, $start_time);
	}

	private function get_sold_goods_ids($start, $end)
	{
		$order_query['enable_distinct'] = TRUE;

        $condition['status'] = $this->get_paidedcondition();

		$order_query['pay_time_greater']= $start;

		$order_query['pay_time_less']	= $end;

		$result		= Order::search($order_query,0,1000);
	
		$goods_ids	= array();

		if($result['count'] > 0)
		{
			foreach($result['list'] as $key => $val)
			{
				if(count($val['goods'] ) > 0)	
				{
					foreach($val['goods'] as $k => $v)
					{
						if(! in_array($v['goods_id'], $goods_ids) && ($v['goods_id'] != self::_reject_goods))
							array_push($goods_ids, $v['goods_id']);
					}
				}
			}
		}
		return $goods_ids;
	}
		
	protected function goods_transacts($start, $end)
	{
		$goods_ids	= $this->get_sold_goods_ids($start, $end);
		if(empty($goods_ids))
			$goods_ids = array('xx');
		$query	= array('id'			=> $goods_ids,
						'shop_status'	=> 'valid',
						'order_by'		=> array('amount_sold'=> 'desc'),
					);
		$query['enable_distinct'] = TRUE;
		
		$goods_datas	= Goods::search($query, $offset, 100);
		
		$count			= $goods_datas['count'];

		$datas			= array();

		$condition		= array();
		if($count > 0)
		{
			$i	= 0;

			$condition['status'] = $this->get_paidedcondition();

			$condition['pay_time_greater']	= $start;

			$condition['pay_time_less']		= $end;

			foreach($goods_datas['list'] as $key => $val)
			{
				$goods_datas['list'][$key]['shop_name']	= Shop::name($goods_datas['list'][$key]['shop_id']);
				
				$datas[$i]['type']		= 1;

				$datas[$i]['target_id']	= $goods_datas['list'][$key]['goods_id'];

				//获得某个商品的交易订单数
				$goods_orders			= $this->get_orders($datas[$i]['target_id'], $condition, $flag = true);	
		
				$datas[$i]['orders']	= $goods_orders['orders'];

				$datas[$i]['goods']		= $goods_orders['goods'];

				$datas[$i]['money']		= $goods_orders['money'] * 100; 
			
				$datas[$i]['buyers']	= $goods_orders['buyers'];
				
				$datas[$i]['time']		= $start;
				
				$i++;
			}
		}
		return empty($datas) ? array() : $datas;
	}

	private function get_orders($key, $condition, $flag = null)
	{
		$condition['enable_distinct'] = TRUE;

		$condition['order_by']		  = array('create_time' => 'desc');
		
		$orders	= Order::search($condition, 0, 10);
	
		$page	= ceil($orders['count']/20);
		
		$user_arr	= array();

		$order_arr	= array();

		$datas['buyers']	= 0;
		
		$datas['goods']		= 0;
	
		$limit	= 20;

		for($i=0; $i< $page; $i++)
		{
			$offset	= $limit * $i;

			$orders	= Order::search($condition, $offset, $limit);
			foreach($orders['list'] as $k => $val)
			{
				//商品数	
				if(count($val['goods']) > 0)
				{
					foreach($val['goods'] as $kk => $v)
					{
						if($key	== $v['goods_id'] && $flag)
						{
							$datas['goods']	+= $v['quantity'];
							$datas['money'] += $v['quantity'] * $v['price'];
							if(!in_array($val['order_id'], $order_arr ))
							{
								$datas['orders']	+= 1;
					
								$order_arr[]	= $val['order_id'];
							}
							//买家数
							if(!in_array($val['member_uid'], $user_arr ))
							{
								$datas['buyers']	+= 1;
					
								$user_arr[]	= $val['member_uid'];
							}
						}elseif(!$flag)
						{
							$datas['goods']	+= $v['quantity'];
							$datas['money'] += $v['quantity'] * $v['price'];
							if(!in_array($val['order_id'], $order_arr ))
							{
								$datas['orders']	+= 1;
					
								$order_arr[]	= $val['order_id'];
							}

							//买家数
							if(!in_array($val['member_uid'], $user_arr ))
							{
								$datas['buyers']	+= 1;
					
								$user_arr[]	= $val['member_uid'];
							}
						}
					}
				}
			}
		}
		return $datas;
	}

	private function get_paidedcondition()
	{
		$refund_status = array_keys(Kohana::config('order.refund.status'));

		$refund_status[] = 'paid';

		$refund_status[] = 'sent';

		$refund_status[] = 'received';

		return $refund_status;
	}
	
	protected function get_unpaidcondition($start_time, $end_time)
	{
		$where = ' and status in (33) and create_time >='.$start_time.' and create_time<='.$end_time;
		return $where;
	}

	protected function get_paidcondition($start_time, $end_time)
	{
		$where	= ' and status in (34,65,66,73) and pay_time >='.$start_time.' and pay_time<='.$end_time;
		return $where;
	}

	protected function get_othercondition($type, $start_time, $end_time)
	{
		$where	= ' and status in ('.$type.') and update_time >='.$start_time.' and update_time<='.$end_time;
		return $where;
	}

	protected function get_refundcondition($type, $start_time, $end_time)
	{
		$arr_type	= explode(',', $type);
		if(in_array('129', $arr_type))
			$where  = ' and status in (0) and create_time >='.$start_time.' and create_time<='.$end_time;
		else
			$where  = ' and status in (1,2) and update_time >='.$start_time.' and update_time<='.$end_time;

		return $where;
	}
	
}
?>
